package com.bqtv.sport.ui.home.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.R
import com.bqtv.sport.adapter.LiveAllStatisticsAdapter
import com.bqtv.sport.adapter.LiveFootballLogoAdapter
import com.bqtv.sport.adapter.LiveImportantAdapter
import com.bqtv.sport.adapter.LiveTextAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.bean.FootballAllStatisticsBean
import com.bqtv.sport.bean.FootballLogoBean
import com.bqtv.sport.commen.widget.GridItemDecoration
import com.bqtv.sport.commen.widget.LinearItemDecoration
import com.bqtv.sport.databinding.FragmentLiveMatchBinding


class LiveMatchFragment : AppFragment<FragmentLiveMatchBinding, AppActivity<ViewBinding>>() {

    private lateinit var gameDecoration: LinearItemDecoration
    private lateinit var importantAdapter: LiveImportantAdapter
    private lateinit var statisticsAdapter: LiveAllStatisticsAdapter
    private lateinit var textAdapter: LiveTextAdapter
    private lateinit var logoAdapter: LiveFootballLogoAdapter
    private var isText = true

    companion object {
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): LiveMatchFragment {
            val fragment = LiveMatchFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): LiveMatchFragment {
            val fragment = LiveMatchFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentLiveMatchBinding.inflate(layoutInflater)

    override fun initView() {
        binding.rcvStatistics.layoutManager = LinearLayoutManager(requireContext())
        statisticsAdapter = LiveAllStatisticsAdapter()
        binding.rcvStatistics.adapter = statisticsAdapter

        binding.rcvGame.layoutManager = LinearLayoutManager(requireContext())
        gameDecoration = LinearItemDecoration(
            getContext(),
            LinearItemDecoration.LineDrawMode.HORIZONTAL, 0f, 10f, 10f
        )
        binding.rcvGame.addItemDecoration(gameDecoration)
        textAdapter = LiveTextAdapter()
        binding.rcvGame.adapter = textAdapter

        importantAdapter = LiveImportantAdapter()

        binding.rcvLogo.layoutManager = GridLayoutManager(requireContext(), 5)
        binding.rcvLogo.addItemDecoration(
            GridItemDecoration(
                getContext(), 5,
                GridItemDecoration.LineDrawMode.HORIZONTAL, 1, 10
            )
        )
        logoAdapter = LiveFootballLogoAdapter()
        binding.rcvLogo.adapter = logoAdapter
    }

    override fun initData() {
        var statistics: MutableList<FootballAllStatisticsBean> = ArrayList()
        statistics.add(
            FootballAllStatisticsBean(
                getString(R.string.possession_rate),
                0,
                0,
                0.0,
                0.0
            )
        )
        statistics.add(FootballAllStatisticsBean(getString(R.string.yellow_card), 0, 0, 0.0, 0.0))
        statistics.add(FootballAllStatisticsBean(getString(R.string.penalty), 0, 0, 0.0, 0.0))
        statistics.add(FootballAllStatisticsBean(getString(R.string.on_target), 0, 0, 0.0, 0.0))
        statistics.add(FootballAllStatisticsBean(getString(R.string.corner), 0, 0, 0.0, 0.0))
        statistics.add(FootballAllStatisticsBean(getString(R.string.red_card), 0, 0, 0.0, 0.0))
        statistics.add(
            FootballAllStatisticsBean(
                getString(R.string.dangerous_attack),
                0,
                0,
                0.0,
                0.0
            )
        )
        statisticsAdapter.submitList(statistics)

        var logos: MutableList<FootballLogoBean> = ArrayList()
        logos.add(FootballLogoBean(R.mipmap.ic_goal, getString(R.string.goal)))
        logos.add(FootballLogoBean(R.mipmap.ic_on_target, getString(R.string.on_target)))
        logos.add(FootballLogoBean(R.mipmap.ic_shot_miss, getString(R.string.shot_miss)))
        logos.add(FootballLogoBean(R.mipmap.ic_penalty, getString(R.string.penalty)))
        logos.add(FootballLogoBean(R.mipmap.ic_missed_penalty, getString(R.string.missed_penalty)))
        logos.add(FootballLogoBean(R.mipmap.ic_own_goal, getString(R.string.own_goal)))
        logos.add(FootballLogoBean(R.mipmap.ic_assist, getString(R.string.assist)))
        logos.add(FootballLogoBean(R.mipmap.corner, getString(R.string.corner)))
        logos.add(FootballLogoBean(R.mipmap.yellow_card, getString(R.string.yellow_card)))
        logos.add(FootballLogoBean(R.mipmap.red_card, getString(R.string.red_card)))
        logos.add(FootballLogoBean(R.mipmap.ic_substitute, getString(R.string.substitute)))
        logos.add(FootballLogoBean(R.mipmap.ic_two_yellows, getString(R.string.two_yellows)))
        logos.add(FootballLogoBean(R.mipmap.ic_var, getString(R.string.varr)))
        logoAdapter.submitList(logos)

        initListener()
    }

    private fun initListener() {
        binding.tdAllStatistics.setOnClickListener {

        }
        binding.tvTextLive.setOnClickListener {
            if (isText) {
                return@setOnClickListener
            }
            isText = true
            binding.tvTextLive.setTextColor(Color.parseColor("#ffffff"))
            binding.tvTextLive.setBackgroundResource(R.drawable.button_theme)
            binding.tvImportantEvents.setTextColor(Color.parseColor("#9E9E9E"))
            binding.tvImportantEvents.setBackgroundResource(R.drawable.bg_round_f6f6f6)
            binding.rcvGame.addItemDecoration(gameDecoration)
            binding.rcvGame.adapter = textAdapter
        }
        binding.tvImportantEvents.setOnClickListener {
            if (!isText) {
                return@setOnClickListener
            }
            isText = false
            binding.tvImportantEvents.setTextColor(Color.parseColor("#ffffff"))
            binding.tvImportantEvents.setBackgroundResource(R.drawable.button_theme)
            binding.tvTextLive.setTextColor(Color.parseColor("#9E9E9E"))
            binding.tvTextLive.setBackgroundResource(R.drawable.bg_round_f6f6f6)
            binding.rcvGame.removeItemDecoration(gameDecoration)
            binding.rcvGame.adapter = importantAdapter
        }
    }
}

